package com.ylkj.freemarkexample;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ylkj.common.entity.LawyerInfo;
import com.ylkj.common.entity.UpCodelist;
import com.ylkj.member.dao.UpCodeListDAO;

import net.sf.json.JSONArray;
  
@Controller  
@RequestMapping("/home")  
public class FreeMarkerController {  
  
	@Autowired
	private UpCodeListDAO upCodeListDAO;
	
    @RequestMapping("/memberList")  
    public ModelAndView memberList(HttpServletRequest request, HttpServletResponse response) {  
  
        User user = new User();  
        user.setUsername("zhangsan");  
        user.setPassword("1234");  
        List<User> users = new ArrayList<User>();  
        users.add(user);  
        return new ModelAndView("member-list", "users", users);  
    }  
    
    @RequestMapping("/index")  
    public ModelAndView Add(HttpServletRequest request, HttpServletResponse response) {  
  
        User user = new User();  
        user.setUsername("zhangsan");  
        user.setPassword("1234");  
        List<User> users = new ArrayList<User>();  
        users.add(user);  
        return new ModelAndView("index", "users", users);  
    }  
    
    
    @RequestMapping("/getLayerInfo")  
    public ModelAndView getLayerInfo(HttpServletRequest request, HttpServletResponse response) {  
  
    	LawyerInfo layer = new LawyerInfo();  
    	layer.setLawyerName("张三"); 
    	layer.setCity("沈阳");  
        List<LawyerInfo> layers = new ArrayList<LawyerInfo>();  
        layers.add(layer);  
        return new ModelAndView("lawyerInfo", "layers", layers);  
    }  
  
    
    @RequestMapping("/codeList")  
    public ModelAndView getCodeListJson(HttpServletRequest request, HttpServletResponse response) {  
    	List<UpCodelist> codelistprovince = upCodeListDAO.getUpCodeList("province");
    	List<CodeListDTO> provinceList = new ArrayList<CodeListDTO>();
    	for (UpCodelist upCodeList : codelistprovince) {
    		CodeListDTO province = new CodeListDTO();
    		province.setCode(upCodeList.getCodeValue());
    		province.setName(upCodeList.getCodeName());
    		List<UpCodelist> codelistcity = upCodeListDAO.getUpCodeListByCity("city", upCodeList.getCodeValue());
    		List<CityDTO> cityList = new ArrayList<CityDTO>();
    		for (UpCodelist upCodeCity : codelistcity) {
    			CityDTO city = new CityDTO();
    			city.setCode(upCodeCity.getCodeValue());
    			city.setName(upCodeCity.getCodeName());
    			city.setPinyin(upCodeCity.getNameSpell());
    			
    			
    			List<UpCodelist> areaList = upCodeListDAO.getUpCodeListByCity("city", upCodeCity.getCodeValue());
    			List<AreaDTO> areaDTOList = new ArrayList<AreaDTO>();
    			for (UpCodelist area : areaList) {
    				AreaDTO areaDto = new AreaDTO();
    				areaDto.setCode(area.getCodeValue());
    				areaDto.setName(area.getCodeName());
    				areaDTOList.add(areaDto);
    			}
    			
    			city.setAreaList(areaDTOList);
    			cityList.add(city);
    		}
    		province.setCityList(cityList);
    		provinceList.add(province);
    	}
    	
    	JSONArray jsonObject = JSONArray.fromObject(provinceList);
    	System.out.println(jsonObject.toString());
        return new ModelAndView("lawyerInfo", "layers", null);  
    }  
} 